<template>
  <div class="class-attendance-view-root">
    <!-- @closed 窗口关闭时结束时的回调   -->
    <el-drawer v-model="classManagerStore.attendanceViewDialogVisible" :title="attendanceViewTitle" size="800"
               @closed="toggleDialog"
               style="text-align: center;"
    >
      <div class="select-date">
        <el-date-picker
            v-model="attendanceDate"
            type="date"
            placeholder="请选择日期"
            value-format="YYYYMMDD"
            size="default"
            @change="dateChange"
        />
      </div>
      <el-table :data="tableData" style="width: 100%;">
        <el-table-column fixed prop="student_info.name" label="学员姓名" width="80" />
        <el-table-column fixed="right" label="考勤情况" min-width="90">
        <template #default="scope">{{ ATTENDANCE_STATUS[scope.row.is_normal===true ? 1 : 0]!==undefined ? ATTENDANCE_STATUS[scope.row.is_normal===true ? 1 : 0] : '其他'}}</template>
      </el-table-column>
        <el-table-column fixed="right" label="异常类型" min-width="120">
          <template #default="scope">{{ sysDictStore.ABNORMAL_TYPE[scope.row.abnormal_type]!==undefined ? sysDictStore.ABNORMAL_TYPE[scope.row.abnormal_type] : '其他'}}</template>
        </el-table-column>
        <el-table-column prop="remark" fixed="right" label="备注" min-width="350"/>
      </el-table>
      <template #footer>
        <div class="dialog-footer">
        </div>
      </template>
    </el-drawer>
  </div>
</template>

<script setup>
import { useClassManagerStore } from "@/store/store-class-manager-pinia";
import {ATTENDANCE_STATUS} from "@/dict/dict-attendance";
import {useSysDictStore} from "@/store/store-sys-dict-pinia";
import {SYS_DICT} from "@/dict/dict-sys";
import {getToday} from "@/utils/util-date";
import {ref} from "vue";
import EventBus from "@/plugins/event-bus";
import {getAttendanceByDateAndClassAPI} from "@/network/attendance/attendance";
import {ElMessage} from "element-plus";

const classManagerStore = useClassManagerStore();
const sysDictStore = useSysDictStore();
sysDictStore.getSysDict([SYS_DICT.ABNORMAL_TYPE]);

// 日期绑定的变量，获取今天的日期为默认值
let attendanceDate=ref(getToday('-'))

let tableData=ref([])
let classId=ref()
let attendanceViewTitle=ref("考勤信息")

const dateChange = (data) =>{
  getAttendanceByDateAndClassAPI(data, classId.value).then(res=>{
    if (res.data.code !== 0) {
      ElMessage.error(res.data.message)
    }
    else {
      tableData.value = res.data.data;
    }
  })
  attendanceDate.value = data
}

const toggleDialog = () => {
  tableData.value = []
  classManagerStore.toggleAttendanceViewDialogVisible();
}

EventBus.on("query-attendance-info-event",(eventData)=>{
  classId.value=eventData.classId
  attendanceViewTitle.value = eventData.className + '考勤信息'
  dateChange(getToday(""))
})

</script>

<style scoped lang="less">
.class-attendance-view-root {
  .select-date {
    text-align: left;
    margin: 10px;
  }
}
</style>